package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.DefaultAddressReq;
import com.ydkj.a37e_mall.Classes.Model.Request.EditAddressReq;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_post;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/1/23.
 * 编辑地址
 */

public class EditAddress extends BaseActivity implements View.OnClickListener {
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private EditText et_receiver, et_phone_number, et_postcode, et_address;
    private TextView tv_location, tv_street, tv_set_default_address, tv_complete;
    private LinearLayout lly_street, lly_location;
    private ImageView img_back;
    private String secret;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_address);
        mActivity = this;
//        initWindow();
        init();
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A000000), 0);

    }

    private void init() {
        myApplication = MyApplication.getInstance();
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);

        tv_street = (TextView) mActivity.findViewById(R.id.tv_street);
        lly_location = (LinearLayout) mActivity.findViewById(R.id.lly_location);
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        et_receiver = (EditText) mActivity.findViewById(R.id.et_receiver);
        et_phone_number = (EditText) mActivity.findViewById(R.id.et_phone_number);
        et_postcode = (EditText) mActivity.findViewById(R.id.et_postcode);
        et_address = (EditText) mActivity.findViewById(R.id.et_address);
        tv_location = (TextView) mActivity.findViewById(R.id.tv_location);
        lly_street = (LinearLayout) mActivity.findViewById(R.id.lly_street);
        tv_set_default_address = (TextView) mActivity.findViewById(R.id.tv_set_default_address);
        tv_complete = (TextView) mActivity.findViewById(R.id.tv_complete);

        if (myApplication.getName() != null) {
            et_receiver.setText(myApplication.getName());
        }
        if (myApplication.getMobile() != null) {
            et_phone_number.setText(myApplication.getMobile());
        }
        if (myApplication.getZip_code() != null) {
            et_postcode.setText(myApplication.getZip_code());
        }
        if (myApplication.getAddress() != null) {
            et_address.setText(myApplication.getAddress());
        }
        if (myApplication.getProvince() != null || myApplication.getCity() != null || myApplication.getArea() != null) {
            tv_location.setText(myApplication.getProvince() + myApplication.getCity() + myApplication.getArea());
        }
        if (myApplication.getLocation() != null) {
            tv_street.setText(myApplication.getLocation());
            Log.e("url", "location==" + myApplication.getLocation());
        }

        img_back.setOnClickListener(this);
        tv_complete.setOnClickListener(this);
        lly_street.setOnClickListener(this);
        lly_location.setOnClickListener(this);
        tv_set_default_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back://返回
                finish();
                break;
            case R.id.tv_complete://完成
                Complete();
                break;
            case R.id.lly_street://街道
                myApplication.setIntent_code(2);
                startActivity(new Intent(EditAddress.this, ChooseAddressLocation.class));
                break;
            case R.id.lly_location://所在地区
                myApplication.setName(et_receiver.getText().toString());
                myApplication.setMobile(et_phone_number.getText().toString());
                myApplication.setZip_code(et_postcode.getText().toString());
                myApplication.setAddress(et_address.getText().toString());
                myApplication.setLocation("");
                myApplication.setIntent_code(2);
                startActivity(new Intent(EditAddress.this, ChooseAddressProvince.class));
                break;
            case R.id.tv_set_default_address://设为默认收货地址
//                SetDefault_Complete();

                SetDefaultAddress();
                break;
        }
    }

    //完成
    private void Complete() {
        CustomProgressDialog.createDialog(mActivity).Show();
        EditAddressReq req = new EditAddressReq();
        req.setId(myApplication.getId());
        req.setProvince(myApplication.getProvince_id() + "");
        req.setCity(myApplication.getCity_id() + "");
        req.setArea(myApplication.getArea_id() + "");
        req.setLocation(myApplication.getLocation_id());
        req.setAddress(et_address.getText().toString());
        req.setZip_code(et_postcode.getText().toString());
        req.setName(et_receiver.getText().toString());
        req.setMobile(et_phone_number.getText().toString());

        String url = Const.COMMON_ADDRESS + req.urlString() + req.getId();
        Log.e("url", "url==" + url);


        Log.e("url", "id==" + req.getId());
        Log.e("url", "access_token==" + sharedPreferencesUtil.getAccessToken());
        Log.e("url", "province==" + req.getProvince());
        Log.e("url", "city==" + req.getCity());
        Log.e("url", "area==" + req.getArea());
        Log.e("url", "location==" + req.getLocation());
        Log.e("url", "address==" + req.getAddress());
        Log.e("url", "zip_code==" + req.getZip_code());
        Log.e("url", "name==" + req.getName());
        Log.e("url", "mobile==" + req.getMobile());

        try {
            String source = req.getString();
            Log.e("url", "source==>" + source);
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }

        params.put("id", req.getId());
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", secret);
        AsyncHttpClient client = new AsyncHttpClient();
        client.put(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "complete_address_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 2);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CommonUtil.showToast(mActivity, "网络连接失败");
                CustomProgressDialog.Dismiss();
            }
        });
    }

    //完成(修改后直接点击设置默认地址)
    private void SetDefault_Complete() {
        CustomProgressDialog.createDialog(mActivity).Show();
        EditAddressReq req = new EditAddressReq();
        req.setId(myApplication.getId());
        req.setProvince(myApplication.getProvince_id() + "");
        req.setCity(myApplication.getCity_id() + "");
        req.setArea(myApplication.getArea_id() + "");
        req.setLocation(myApplication.getLocation_id());
        req.setAddress(et_address.getText().toString());
        req.setZip_code(et_postcode.getText().toString());
        req.setName(et_receiver.getText().toString());
        req.setMobile(et_phone_number.getText().toString());

        String url = Const.COMMON_ADDRESS + req.urlString() + req.getId();
        Log.e("url", "url==" + url);

        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        params.put("id", req.getId());
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", secret);
        AsyncHttpClient client = new AsyncHttpClient();
        client.put(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "complete_address_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    SetDefaultAddress();
                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 2);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CommonUtil.showToast(mActivity, "网络连接失败");
                CustomProgressDialog.Dismiss();
            }
        });
    }

    //设为默认收货地址
    private void SetDefaultAddress() {

        CustomProgressDialog.createDialog(mActivity).Show();
        DefaultAddressReq req = new DefaultAddressReq();

        req.setUuid(myApplication.getDataEntity().getUuid());

        String url = Const.COMMON_ADDRESS + req.urlString() + myApplication.getId();
        RequestParams params = new RequestParams();


        try {
            String source = req.getString();
            Log.e("ssss", "source==>" + source);

            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("ssss", "id==>" + myApplication.getId());
        Log.e("ssss", "token==>" + sharedPreferencesUtil.getAccessToken());
        params.put("id", myApplication.getId());
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", secret);
        Log.e("url", "params==" + params.toString());
        Async_http_post.post(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "default_adderss_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 3);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CommonUtil.showToast(mActivity, "网络连接失败");
                CustomProgressDialog.Dismiss();
            }
        });
    }

}
